package com.quick.service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.quartz.Scheduler;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @ClassName JobService
 * @Description TODO
 * @Author wangxin332
 * @Date 2018/10/19 下午12:41
 * @Version 1.0
 **/
@Service
public class JobService {
	@Resource
	private Map<String, Scheduler> schedulerMap;

	/**
	 * 暂停
	 * @param schedulerName
	 */
	public void pauseJob (String schedulerName){
		try {
			Scheduler scheduler = schedulerMap.get(schedulerName);
			scheduler.standby();
		}catch (Exception e){
			e.printStackTrace();
		}
	}

	/**
	 * 启动
	 * @param schedulerName
	 */
	public void startJob(String schedulerName){
		try {
			Scheduler scheduler = schedulerMap.get(schedulerName);
			scheduler.start();
		}catch (Exception e){
			e.printStackTrace();
		}
	}

	/**
	 * 判断是否启动
	 * @param schedulerName
	 * @return
	 */
	public boolean isStarted(String schedulerName){
		boolean flag = Boolean.FALSE;
		try {
			Scheduler scheduler = schedulerMap.get(schedulerName);
			flag = scheduler.isStarted();
		}catch (Exception e){
			e.printStackTrace();
		}
		return flag;
	}

	public List<Map> getJob(){
		List<Map> list = Lists.newArrayList();
		try {
			for (String key : schedulerMap.keySet()) {
				Scheduler scheduler = schedulerMap.get(key);
				boolean flag = scheduler.isStarted();
				Map<String,Object> temp = Maps.newLinkedHashMap();
				temp.put("schedulerName",key);
				temp.put("isStarted",flag);
				list.add(temp);
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return list;
	}

}

